<script setup>
// 引入挂载生命周期
import {onMounted, onUpdated, ref, reactive} from 'vue'

import request from '../utils/axios'
import {defineSchedule} from '../store/scheduleStore'
import {defineUser} from '../store/userStore'

let schedule = defineSchedule()
let sysUser = defineUser()

// 第一次挂载就立刻向后端发送请求，获取最新数据
onMounted(async function(){
    showSchedule()
})

async function showSchedule(){
    let {data} = await request.get("/schedule/findAllScheduleByUid",
                            {params:{"uid":sysUser.uid}})
    console.log(data)
    schedule.itemList = data.data.itemList
}

async function addSchedule(){
   let {data} = await request.get("/schedule/addScheduleByUid",
                                {params:{"uid":sysUser.uid}})
    if(data.code == 200){
        // 新增成功，刷新页面，获取最新数据
        showSchedule()
    }else{
        alert("添加异常")
    }
}

async function updateSchedule(index){
    let {data} = await request.post("/schedule/updateSchedule",schedule.itemList[index])
    if(data.code == 200){
        alert("更新成功")
        showSchedule()
    }else{
        alert("更新异常")
    }
}

async function deleteSchedule(index){
    let sid = schedule.itemList[index].sid
    let {data} = await request.get("/schedule/deleteSchedule",
                                {params:{"sid":sid}})
    if(data.code == 200){
        showSchedule()
    }else{
        alert("删除失败")
    }
}

</script>

<template>
  <div>
    <h3 class="ht">您的日程如下</h3>
<table class="tab" cellspacing="0px">
    <tr class="ltr">
        <th>编号</th>
        <th>内容</th>
        <th>进度</th>
        <th>操作</th>
    </tr>
    <tr class="ltr" v-for="(item,index) in schedule.itemList" :key="index">
        <td v-text="index+1"></td>
        <td>
            <input type="input" v-model="item.title">
        </td>
        <td>
            <input type="radio" value="1" v-model="item.completed"> 已完成
            <input type="radio" value="0" v-model="item.completed"> 未完成
        </td>
        <td class="buttonContainer">
             <button class="btn1" @click="deleteSchedule(index)">删除</button>
             <!-- 通过index指导修改的是哪个日程 -->
            <button class="btn1" @click="updateSchedule(index)">保存修改</button>
        </td>
    </tr>
    <tr class="ltr buttonContainer" >
        <td colspan="4">
            <button class="btn1" @click="addSchedule()">新增日程</button>
        </td>

    </tr>
</table>
  </div>
</template>

<style scoped>

      .ht{
            text-align: center;
            color: cadetblue;
            font-family: 幼圆;
        }
        .tab{
            width: 80%;
            border: 5px solid cadetblue;
            margin: 0px auto;
            border-radius: 5px;
            font-family: 幼圆;
        }
        .ltr td{
            border: 1px solid  powderblue;

        }
        .ipt{
            border: 0px;
            width: 50%;

        }
        .btn1{
            border: 2px solid powderblue;
            border-radius: 4px;
            width:100px;
            background-color: antiquewhite;

        }
        #usernameMsg , #userPwdMsg {
            color: gold;
        }

        .buttonContainer{
            text-align: center;
        }

</style>